package com.endava.spring_ioc.controller;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.endava.spring_ioc.beans.Account;
import com.endava.spring_ioc.dao.DataBase;
import com.endava.spring_ioc.service.AccountService;

/**
 * The class of test.
 * 
 * @author acojocar
 * @since 26.07.2013
 * @version 1.0
 * 
 */
public class MainController {

	public static void main(String[] args) {

		BeanFactory factory = new ClassPathXmlApplicationContext(
				"appContext.xml");

		AccountService accountService = (AccountService) factory
				.getBean("accountService");

		// adding/populating Data Base
		accountService.addAccount(1, "saving_account", 2500);
		accountService.addAccount(2, "spending_account", 1200);
		accountService.addAccount(3, "spending_account", 30200);
		accountService.addAccount(4, "saving account", 2400);
		accountService.addAccount(5, "spending account", 21100);

		// delete from Data Base
//		accountService.deleteAccount(4);
//		accountService.deleteAccount(5);

		List<Account> accounts = accountService.getAllAccounts();

		for (Account acc : accounts) {
			System.out.println(acc.toString());
		}

		try {
			DataBase.getInstance().getDB().close();
			System.out.println(" > Connection closed.");
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
